package cn.well.cloud.modular.controller;

import cn.hutool.core.util.StrUtil;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.kernel.model.response.ResponseData;
import cn.well.cloud.base.enums.WXTypeEnums;
import cn.well.cloud.base.wechat.api.impl.AppWXUser;
import cn.well.cloud.modular.model.CourseInfo;
import cn.well.cloud.modular.model.UserInfo;
import cn.well.cloud.modular.service.ICourseInfoService;
import cn.well.cloud.modular.service.IUserInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

/**
 * 微信授权登录
 *
 * @author jiawei
 * @Date 2020-05-26 20:13:33
 */
@Controller
@RequestMapping("/wxauth")
public class WXAuthController extends BaseController {

    private String PREFIX = "/wxauth/";

    @Autowired
    private ICourseInfoService courseInfoService;

    @Autowired
    private IUserInfoService userInfoService;

    AppWXUser appWXUser = new AppWXUser();
    /**
     * 跳转到微信登录页
     */
    @RequestMapping("/htologyLogin")
    public String htologyLogin(@RequestParam(value = "openId", required = false) String openId,
                                @RequestParam(value = "headUrl", required = false) String headUrl,
                                @RequestParam(value = "nickName", required = false) String nickName) {
        if(StrUtil.isNotEmpty(openId)){
            //用户授权后，保存/更新用户信息
            userInfoService.saveUserInfo(openId,headUrl,nickName, WXTypeEnums.HTOLOGY);
            return PREFIX + "htologyLogin.html";
        }else{
            return PREFIX + "htologyLogin.html";
        }
    }

    /**
     * 跳转到微信登录页
     */
    @RequestMapping("/oncoLogin")
    public String oncoLogin(@RequestParam(value = "openId", required = false) String openId,
                               @RequestParam(value = "headUrl", required = false) String headUrl,
                               @RequestParam(value = "nickName", required = false) String nickName) {
        if(StrUtil.isNotEmpty(openId)){
            //用户授权后，保存/更新用户信息
            userInfoService.saveUserInfo(openId,headUrl,nickName, WXTypeEnums.ONCO);
            return PREFIX + "oncoLogin.html";
        }else{

            return PREFIX + "oncoLogin.html";
        }
    }


    /**
     *  验证用户是登录注册过
     */
    @RequestMapping(value = "/checkLogin")
    @ResponseBody
    public Object checkLogin(@RequestParam("openId") String openId) {
        QueryWrapper<UserInfo> qw = new QueryWrapper<UserInfo>();
        UserInfo userInfo = new UserInfo();
        userInfo.setOpenId(openId);
        qw.setEntity(userInfo);
        UserInfo result = userInfoService.getOne(qw);
        if(result !=null){
            return ResponseData.success(true);
        }else{
            return ResponseData.success(false);
        }
    }

    /**
     *  跳转到验证是否关注公众号页面,用户非静默授权
     * @param courseId
     * @return
     */
    @RequestMapping(value = "/oncoSpread")
    public String spread(@RequestParam(value = "courseId", required = true) Long courseId,
                         @RequestParam(value = "openId", required = false) String openId,
                         @RequestParam(value = "headUrl", required = false) String headUrl,
                         @RequestParam(value = "nickName", required = false) String nickName,
                         HttpServletRequest request) {
        CourseInfo courseInfo=courseInfoService.getById(courseId);
        if(courseInfo==null){
            return  "404.html";
        }
        request.setAttribute("courseInfo",courseInfo);
        return PREFIX + "oncoDYSpread.html"; //onco订阅号
    }

    /**
     *  验证用户是否关注,若已关注，返回课程链接
     */
    @RequestMapping(value = "/checkSubscribe")
    @ResponseBody
    public Object checkSubscribe(@RequestParam(value ="openId", required = true) String openId,
                                             @RequestParam(value ="courseId", required = true) String courseId,
                                              HttpServletRequest request) {
        Map<String,Object> result = new HashMap<String,Object>();
        return ResponseData.success(result);
    }



    /**
     *  用户关注,third服务远程调用
     */
    @RequestMapping(value = "/subscribe")
    @ResponseBody
    public Object subscribe(@RequestParam("appId") String appId, @RequestParam("openId") String openId) {
        return ResponseData.success(true);
    }

    /**
     *  用户取消关注服务远程调用
     */
    @RequestMapping(value = "/unsubscribe")
    @ResponseBody
    public Object unsubscribe(@RequestParam("appId") String appId, @RequestParam("openId") String openId) {
        UserInfo result = userInfoService.updateUnSubscribeUserInfo(appId,openId);
        if(result !=null){
            return ResponseData.success(true);
        }else{
            return ResponseData.success(false);
        }
    }

}
